import QtQuick 2.2
import "../mainmenu"
import Qt.database.sqlite 1.0
import Control.Class 1.0

Rectangle{

    // width: 150; height: 250
    z: 0.9

    signal insertInstruction(string instructClass, string instruction )
    border.color: "black"; border.width: 1

    Component.onCompleted: {
        inspanel.settitleList("select Title from Instructions where Type = \'Common\'");
        for(var i=0; i< inspanel.strCount(); i++)
        {
            instructItem.textnameList.push(inspanel.getTitle(i))
            console.log(instructItem.textnameList[i] + "   ")
        }


       console.log("Common中的命令有"+inspanel.strCount()+"条")
    }

    //逻辑类
    InstructionPanel{
        id: inspanel
    }

    //集合栏
    InstructItem{
        id: options
        // height: 80
        //color: "#C0C0C0"
        backcolor: "#C0C0C0"; clickcolor: "white"
        border.color: "black"; border.width: 1
        textname: "Common";
        textHLocate: 1
        textVLocate: 1
        // backcolor: "red"
        //  textHLocate: 1; textVLocate: 1

        //命令类名称改变 titleChanged
        onTitleChanged: {
            instructItem.itemCount = 0;

            inspanel.settitleList("select Title from Instructions where Type = \'"+title + "\'");
            console.log(title+"中的命令有"+inspanel.strCount()+"条")
            //先清空原数组
//            instructItem.textnameList.splice(0, instructItem.textnameList.length)
            instructItem.textnameList.splice(0, instructItem.textnameList.length)

            for(var i=0; i< inspanel.strCount(); i++)
            {
                instructItem.textnameList.push(inspanel.getTitle(i))

                console.log(instructItem.textnameList[i] + "   ")
            }
            //初始化是否可以下翻页
            if(instructItem.textnameList.length > 10)
                instructItem.nextFlag = true
            else
                instructItem.nextFlag = false

            instructItem.previousFlag = false

            instructItem.resetText()
//            instructItem.update()


        }

        anchors{
            top: parent.top; left: parent.left; right: parent.right
        }
    }

    //面板
    Rectangle{
        id: panel
        anchors{
            top: options.bottom; bottom: parent.bottom; left:parent.left; right: parent.right;
            leftMargin: 3
        }
        //color: "blue"
        //命令大类集合面板
        InstructClassPanel{id: instruct; visible: false;
            anchors.fill: parent; anchors.bottomMargin: 5
            onOnclick: {
                options.textname = title;
                options.isSelect = false;
                options.color = options.backcolor
                instruct.visible = false;
            }
        }

        //命令条目面板
        ModelPanel{
            id: instructItem
            itemBorderColor: qsTr("green")
            rowspacing: 10
            columnspacing: 20;
            anchors{
                top: parent.top; left: parent.left; right: parent.right; bottom: btnbox.top
            }

            MouseArea{
                anchors.fill: parent
                onClicked: {
                    insertInstruction(options.textname, instructItem.getItemfromPoint(mouse.x, mouse.y - 10))
                    console.log(options.textname + "——————" + instructItem.getItemfromPoint(mouse.x, mouse.y-10))
                }
            }
        }

        //上一条下一条的按钮
        Rectangle{
            anchors{
                bottom: parent.bottom; left: parent.left; right: parent.right
            }
            id: btnbox
            height: 50;
            Row{                
                spacing: 5;
                InstructItem{id: previous; textname: qsTr("<—— 上一个");
                    textHLocate: 1; textVLocate: 1; width: (parent.width- 5*3)/2;
                    bordercolor: qsTr("green"); borderwidth: 1; presscolor: qsTr("gray")
                    onOnclick: {
                        if(instructItem.previousFlag)
                        {
                            instructItem.nextFlag = true
                            instructItem.itemCount -= 10

                            if(instructItem.itemCount < 0+10)
                                instructItem.previousFlag = false
                        }

                        instructItem.resetText()
                    }

                }
                InstructItem{id: next; textname: qsTr("下一个 ——>");
                    textHLocate: 1; textVLocate: 1; width: (parent.width- 5*3)/2;
                    bordercolor: qsTr("green"); borderwidth: 1; presscolor: qsTr("gray")

                    onOnclick: {
                        if(instructItem.nextFlag)
                        {
                            instructItem.previousFlag = true;
                            instructItem.itemCount+=10;

                            if(instructItem.itemCount +10 >=instructItem.textnameList.length)
                                instructItem.nextFlag = false
                        }
                        instructItem.resetText()
                    }
                }
                anchors.fill: parent;            
            }
        }

        //选择命令大类
        Connections{
            target: options
            onOnclick:{
                if(click)
                {
                    instruct.visible = true
                    instruct.z = 0.9
                    console.log(qsTr("Initail Instruct Panel successfully!"))
                    instruct.initInstructClass()
                    instruct.searchTitle(title)
                }
                else
                {
                    instruct.visible = false
                    instruct.z = 0;
                }
            }
        }      
    }

    //指令面板
    Rectangle{
        id: instructPanel
    }
}
